﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ImportMFGsFromFile.CUiImportMFGsFromFileDialog
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.ImportMFGsFromFile;

public class CUiImportMFGsFromFileDialog : TxWindow, INotifyPropertyChanged, IComponentConnector
{
  private double _progressBarPercent;
  private string _currentMessageToDisplay = "";
  private string _statMsg = "";
  private bool _canClose = true;
  private bool _existsLogFile;
  private bool _canImportFile;
  private bool _isInputFileSetViaBrowseDialog;
  private BitmapImage _statIcon;
  private CApImportMFGsFromFileInternal _logic;
  private string[] m_csvFileNames;
  internal CUiImportMFGsFromFileDialog importMFGsDlg;
  internal System.Windows.Controls.ListBox lstFilePaths;
  internal System.Windows.Controls.CheckBox chUseWorkingFrame;
  internal StackPanel statusBar;
  private bool _contentLoaded;

  public CUiImportMFGsFromFileDialog()
  {
    this.InitializeComponent();
    this._logic = new CApImportMFGsFromFileInternal();
  }

  public void InnerExecute()
  {
    this.RestorePersistenceValuesFromRegistry();
    ((Window) this).Show();
    this.InitHandlers();
  }

  public bool CanCloseDialog
  {
    get => this._canClose;
    set
    {
      this._canClose = value;
      this.FirePropertyChanged(nameof (CanCloseDialog));
    }
  }

  public bool CanImportFile
  {
    get => this._canImportFile;
    set
    {
      this._canImportFile = value;
      this.FirePropertyChanged(nameof (CanImportFile));
    }
  }

  public bool ExistsLogFile
  {
    get => this._existsLogFile;
    set
    {
      this._existsLogFile = value;
      this.FirePropertyChanged(nameof (ExistsLogFile));
    }
  }

  public double ProgressBarValue
  {
    get => this._progressBarPercent;
    set
    {
      this._progressBarPercent = value;
      this.FirePropertyChanged(nameof (ProgressBarValue));
    }
  }

  public string CurrentMessage
  {
    get => this._currentMessageToDisplay;
    set
    {
      this._currentMessageToDisplay = value;
      this.FirePropertyChanged(nameof (CurrentMessage));
    }
  }

  public BitmapImage StatusIcon
  {
    get => this._statIcon;
    private set
    {
      this._statIcon = value;
      this.FirePropertyChanged(nameof (StatusIcon));
    }
  }

  public string StatusMessage
  {
    get => this._statMsg;
    set
    {
      this._statMsg = value;
      this.FirePropertyChanged(nameof (StatusMessage));
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void FirePropertyChanged(string prop)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(prop));
    System.Windows.Forms.Application.DoEvents();
  }

  private void InitHandlers()
  {
    this._logic.onImportStatusChanged += new OnImportStatusChangedEventHandler(this.logic_onImportStatusChanged);
    this._logic.onErrorDetected += new OnErrorDetectedEventHandler(this.logic_onErrorDetected);
    this._logic.onMFGImported += new OnMFGImportedEventHandler(this.logic_onMFGImported);
    this._logic.onWarningDetected += new OnWarningDetectedEventHandler(this.logic_onWarningDetected);
  }

  private void SetVerticalSizeToContent()
  {
    ((FrameworkElement) this).MinHeight = 0.0;
    ((FrameworkElement) this).MaxHeight = double.PositiveInfinity;
    ((Window) this).SizeToContent = SizeToContent.Height;
    ((UIElement) this).UpdateLayout();
    ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).ActualHeight;
  }

  private void logic_onImportStatusChanged(object sender, OnImportStatusChangedEventArgs args)
  {
    switch (args.Status)
    {
      case ImportStatus.Initializing:
        this.CurrentMessage = this._logic.ResourceMgr.GetString("IMFF_INITIALIZING");
        break;
      case ImportStatus.ParsingFile:
        this.CurrentMessage = this._logic.ResourceMgr.GetString("IMFF_PARSING");
        this.ProgressBarValue += (double) (20 / this.m_csvFileNames.Length);
        break;
      case ImportStatus.ImportingMFGs:
        this.CurrentMessage = this._logic.ResourceMgr.GetString("IMFF_IMPORTING_MFGS");
        break;
      case ImportStatus.Finished:
        this.CurrentMessage = this._logic.GetSummaryMessage();
        this.ProgressBarValue = 100.0;
        break;
    }
  }

  private void logic_onWarningDetected(object sender, EventArgs args)
  {
    if (this.StatusIcon != this._logic.SuccessIcon)
      return;
    this.StatusIcon = this._logic.WarningIcon;
  }

  private void logic_onErrorDetected(object sender, EventArgs args)
  {
    this.StatusIcon = this._logic.ErrorIcon;
  }

  private void logic_onMFGImported(object sender, EventArgs args)
  {
    this.ProgressBarValue += 80.0 / (double) this._logic.LinesParsed;
  }

  private void FinalizeHandlers()
  {
    this._logic.onErrorDetected -= new OnErrorDetectedEventHandler(this.logic_onErrorDetected);
    this._logic.onMFGImported -= new OnMFGImportedEventHandler(this.logic_onMFGImported);
    this._logic.onWarningDetected -= new OnWarningDetectedEventHandler(this.logic_onWarningDetected);
  }

  private void UpdateSize()
  {
    ((FrameworkElement) this).MinHeight = 0.0;
    ((FrameworkElement) this).MaxHeight = double.PositiveInfinity;
    ((Window) this).SizeToContent = SizeToContent.WidthAndHeight;
    ((UIElement) this).UpdateLayout();
    ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).ActualHeight;
  }

  private void StorePersistenceValuesFromRegistry()
  {
    TxWindowSettings.StoreWindowValue((TxWindow) this, "LastBrowsedDirectory", (object) this._logic.LastBrowsedDirectory);
    TxWindowSettings.StoreWindowValue((TxWindow) this, "UseWorkingFrame", (object) this.chUseWorkingFrame.IsChecked);
  }

  private void RestorePersistenceValuesFromRegistry()
  {
    object obj = (object) null;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "UseWorkingFrame", ref obj);
    bool result;
    if (obj != null && bool.TryParse(obj.ToString(), out result))
      this.chUseWorkingFrame.IsChecked = new bool?(result);
    else
      this.chUseWorkingFrame.IsChecked = new bool?(false);
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "LastBrowsedDirectory", ref obj);
    if (!(obj is string))
      return;
    this._logic.LastBrowsedDirectory = obj as string;
  }

  private void importMFGsDlg_Closing(object sender, CancelEventArgs e)
  {
    if (this.CanCloseDialog)
      this.FinalizeHandlers();
    else
      e.Cancel = true;
  }

  private void importMFGsDlg_Loaded(object sender, RoutedEventArgs e) => this.UpdateSize();

  private void Button_Show_Browse_Click(object sender, RoutedEventArgs e)
  {
    this.GetFilePathFromBrowseDialog();
  }

  private void GetFilePathFromBrowseDialog()
  {
    this.m_csvFileNames = this._logic.GetFilesPath();
    if (this.m_csvFileNames == null || this.m_csvFileNames.Length == 0)
      return;
    string str = "";
    for (int index = 0; index < this.m_csvFileNames.Length; ++index)
    {
      str = $"{str}\"{this.m_csvFileNames[index]}\"";
      if (index < this.m_csvFileNames.Length - 1)
        str += ",";
    }
    this.lstFilePaths.ItemsSource = (IEnumerable) this.m_csvFileNames;
    this._isInputFileSetViaBrowseDialog = true;
    this.CanImportFile = true;
  }

  private void Button_Open_Log_Click(object sender, RoutedEventArgs e)
  {
    this._logic.OpenLogFile();
    ((Window) this).Close();
  }

  private void Button_Import_Click(object sender, RoutedEventArgs e)
  {
    this.CanImportFile = false;
    this.ExistsLogFile = false;
    this.statusBar.Visibility = Visibility.Visible;
    this.UpdateSize();
    this.ImportFiles();
    this.lstFilePaths.ItemsSource = (IEnumerable) null;
    this.m_csvFileNames = (string[]) null;
    this.ExistsLogFile = true;
    this.StorePersistenceValuesFromRegistry();
  }

  private void ImportFiles()
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    try
    {
      this.ProgressBarValue = 0.0;
      this.StatusIcon = this._logic.SuccessIcon;
      this._logic.UseWorkingFrame = this.chUseWorkingFrame.IsChecked.Value;
      CApImportMFGsAnalytics.Start();
      this.CanCloseDialog = false;
      this._logic.ImportFiles(this.m_csvFileNames);
      this.CanCloseDialog = true;
    }
    finally
    {
      Cursor.Current = current;
      CApImportMFGsAnalytics.End();
    }
  }

  private void Button_Close_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/importmfgsfromfile/cuiimportmfgsfromfiledialog.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.importMFGsDlg = (CUiImportMFGsFromFileDialog) target;
        ((Window) this.importMFGsDlg).Closing += new CancelEventHandler(this.importMFGsDlg_Closing);
        ((FrameworkElement) this.importMFGsDlg).Loaded += new RoutedEventHandler(this.importMFGsDlg_Loaded);
        break;
      case 2:
        this.lstFilePaths = (System.Windows.Controls.ListBox) target;
        break;
      case 3:
        this.chUseWorkingFrame = (System.Windows.Controls.CheckBox) target;
        break;
      case 4:
        ((System.Windows.Controls.Primitives.ButtonBase) target).Click += new RoutedEventHandler(this.Button_Show_Browse_Click);
        break;
      case 5:
        this.statusBar = (StackPanel) target;
        break;
      case 6:
        ((System.Windows.Controls.Primitives.ButtonBase) target).Click += new RoutedEventHandler(this.Button_Import_Click);
        break;
      case 7:
        ((System.Windows.Controls.Primitives.ButtonBase) target).Click += new RoutedEventHandler(this.Button_Open_Log_Click);
        break;
      case 8:
        ((System.Windows.Controls.Primitives.ButtonBase) target).Click += new RoutedEventHandler(this.Button_Close_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
